package com.cc8w;

import com.cc8w.service.InetAddressService;
import com.cc8w.service.UrlService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
import java.util.Arrays;


/**
 * 第二种非Web方法 :
 * 1.启动类集成 CommandLineRunner 接口
 * 2.重写run方法,注解形式@Autowired加入bean
 * 3.重写的run方法可理解为程序入口
 */
@SpringBootApplication
public class JavaNetApplication implements CommandLineRunner {

    @Autowired
    private InetAddressService inetAddressService;
    
    @Autowired
    private UrlService urlService;
    


    public static void main(String[] args) {
        SpringApplication.run(JavaNetApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
    	//TestInetAddress();
    	TestUrl();
    }
    
    
    //测试InetAddressService类
    public void TestInetAddress() throws UnknownHostException {

        System.out.println(inetAddressService.getHostName());
        System.out.println(inetAddressService.getHostAddress());
        //有个知识点,数组转字符串
        System.out.println(Arrays.toString(inetAddressService.getBtyeAddress()));
        inetAddressService.getNameByIp();
    	
    }
    
    //测试URL类
    public void TestUrl() throws IOException {
    	
    	urlService.setUrl("https://www.baidu.com");
    	//1.获得字节流
    	InputStream is_html = urlService.getUrlByte();
    	//2.转化成字符流(用于输出)
    	InputStreamReader isr = new InputStreamReader(is_html);
    	//3.把字符流转出还差字符流(提高效率)
    	BufferedReader br = new BufferedReader(isr);
    	//4.输出打印
    	String b = br.readLine();
    	while(b!=null) {
    		System.out.println(b);
    		b = br.readLine();	
    	};
    	
    }




}
